package cn.qn.flowable.controller;

import cn.qn.flowable.result.BaseResponse;
import lombok.RequiredArgsConstructor;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProcessInstanceController {
    private final RepositoryService repositoryService;
    private final RuntimeService runtimeService;
    private final TaskService taskService;

    /**
     * 获取流程定义
     */
    @GetMapping("/process/definition")
    public ResponseEntity<BaseResponse<List<Map<String, Object>>>> getProcessDefinitions(@RequestParam String deploymentId) {
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .latestVersion()
                .list();

        List<Map<String, Object>> result = new ArrayList<>();
        if (!list.isEmpty()) {
            list.forEach(processDefinition -> {
                Map<String, Object> map = new HashMap<>();
                map.put("id", processDefinition.getId());
                map.put("name", processDefinition.getName());
                map.put("key", processDefinition.getKey());
                map.put("version", processDefinition.getVersion());
                result.add(map);
            });
        }
        return new ResponseEntity<>(new BaseResponse<>(result), HttpStatus.OK);
    }


    /**
     * 用户审批
     */
    private final String ASSIGNEE_USER_KEY = "assignee_user";
    /**
     * 专业负责人
     */
    private final String ASSIGNEE_MANAGER_KEY = "assignee_manager";
    /**
     * 业务副总
     */
    private final String ASSIGNEE_BUSSINESS_MANAGER_KEY = "assignee_bussiness_manager";
    /**
     * 行政副总
     */
    private final String ASSIGNEE_ADMIN_MANAGER_KEY = "assignee_admin_manager";
    /**
     * 总经理
     */
    private final String ASSIGNEE_GENERAL_MANAGER_KEY = "assignee_general_manager";

    /**
     * 启动流程.
     *
     * @return 流程key
     */
    @GetMapping("/start")
    public ResponseEntity<Object> startProcessDef(@RequestParam String processId) {
        //1.维护该流程的变量
        Map<String, Object> variables = new HashMap<>();
        variables.put(ASSIGNEE_USER_KEY, 1);
        variables.put(ASSIGNEE_MANAGER_KEY, 2);
        variables.put(ASSIGNEE_BUSSINESS_MANAGER_KEY, 3);
        variables.put(ASSIGNEE_ADMIN_MANAGER_KEY, 4);
        variables.put(ASSIGNEE_GENERAL_MANAGER_KEY, 5);
        //2.启动流程实例，第一个参数是流程定义的id
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceById(processId, variables);
        //3.返回流程实例对象id
        return new ResponseEntity<>(new BaseResponse<>(processInstance.getId()), HttpStatus.OK);
    }

}
